package com.xiaozhen.alarm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.alarm.dao.AlarmConfigDao;
import com.xiaozhen.alarm.dao.AlarmConfigStrategyDao;
import com.xiaozhen.alarm.domain.AlarmConfig;
import com.xiaozhen.alarm.domain.AlarmConfigStrategy;
import com.xiaozhen.alarm.domain.AlarmNotifyUser;
import com.xiaozhen.alarm.domain.queryParams.AlarmConfigParam;
import com.xiaozhen.alarm.service.AlarmConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/10/25 9:21
 * @description:
 */
@Service
public class AlarmConfigServiceImpl implements AlarmConfigService {

    @Autowired
    private AlarmConfigDao alarmConfigDao;
    @Autowired
    private AlarmConfigStrategyDao alarmConfigStrategyDao;

    @Override
    public Page<AlarmConfig> selectAlarmConfigList(Page<AlarmConfig> page, AlarmConfigParam param) {
        return alarmConfigDao.selectAlarmConfigList(page, param);
    }

    @Override
    public int insertAlarmConfig(AlarmConfig alarmConfig) {
        int num = alarmConfigDao.insert(alarmConfig);
        if (num > 0) {
            List<Long> strategyIds = alarmConfig.getStrategyIds();
            Long configId = alarmConfig.getConfigId();
            for (Long strategyId : strategyIds) {
                AlarmConfigStrategy alarmConfigStrategy = new AlarmConfigStrategy();
                alarmConfigStrategy.setStrategyId(strategyId);
                alarmConfigStrategy.setConfigId(configId);
                alarmConfigStrategyDao.insert(alarmConfigStrategy);
            }
        }
        return num;
    }

    @Override
    public int updateAlarmConfig(AlarmConfig alarmConfig) {
        int num = alarmConfigDao.updateById(alarmConfig);
        if (num > 0) {
            Long configId = alarmConfig.getConfigId();
            QueryWrapper<AlarmConfigStrategy> configStrategyQueryWrapper = new QueryWrapper<>();
            configStrategyQueryWrapper.eq("config_id", configId);
            alarmConfigStrategyDao.delete(configStrategyQueryWrapper);
            List<Long> strategyIds = alarmConfig.getStrategyIds();
            for (Long strategyId : strategyIds) {
                AlarmConfigStrategy alarmConfigStrategy = new AlarmConfigStrategy();
                alarmConfigStrategy.setStrategyId(strategyId);
                alarmConfigStrategy.setConfigId(configId);
                alarmConfigStrategyDao.insert(alarmConfigStrategy);
            }
        }
        return num;
    }

    @Override
    public int deleteAlarmConfigById(Long configId) {
        int num = alarmConfigDao.deleteById(configId);
        if (num > 0) {
            QueryWrapper<AlarmConfigStrategy> configStrategyQueryWrapper = new QueryWrapper<>();
            configStrategyQueryWrapper.eq("config_id", configId);
            alarmConfigStrategyDao.delete(configStrategyQueryWrapper);
        }
        return num;
    }

    @Override
    public List<AlarmConfig> selectAlarmConfigByTableName(String tableName) {
        QueryWrapper<AlarmConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_name", tableName);
        return alarmConfigDao.selectList(queryWrapper);
    }
}
